#include "mqtt/mqtt.h"
#include "JSON/cJSON.h"
#include "MQTTProcess.h"
#include "PCM/pcm.h"
#include "Common.h"
#include "globaldata.h"
static char *get_mqtt_uuid()
{
	static char uuidstr[16];
	static unsigned int uuidcnt = 0;
	snprintf(uuidstr, sizeof(uuidstr), "DUID%d", uuidcnt++);
	return uuidstr;
}

static char *get_xintiao_msgbody(char *uuid)
{
	char *p;
	cJSON *pJsonRoot = NULL;
	cJSON *pJsonData = NULL;
	char tmpstr[32];

	pJsonRoot = cJSON_CreateObject();
	pJsonData = cJSON_CreateObject();
	if (NULL == pJsonRoot)
	{
		goto err;
	}
	if (NULL == pJsonData)
	{
		goto err;
	}

	cJSON_AddStringToObject(pJsonRoot, "DEVID", DEVID);
	cJSON_AddNumberToObject(pJsonRoot, "DEVTYPE", 3); //摄像头消息
	cJSON_AddStringToObject(pJsonRoot, "MSGID", uuid);
	cJSON_AddNumberToObject(pJsonRoot, "MSGCODE", 80300);
	cJSON_AddNumberToObject(pJsonRoot, "SIGNAL", 28);
	cJSON_AddNumberToObject(pJsonRoot, "VOLTAGE", 3600);
	cJSON_AddNumberToObject(pJsonRoot, "NETWORK", 0);
	cJSON_AddNumberToObject(pJsonRoot, "STREAM",rt_data.stream_state);
	cJSON_AddNumberToObject(pJsonRoot, "STORAGE", 0);
	cJSON_AddNumberToObject(pJsonRoot, "AUDIO_ENABLE", rt_data.audio_enable);
	cJSON_AddNumberToObject(pJsonRoot, "VIDEO_ENALBE", rt_data.video_enable);
	cJSON_AddNumberToObject(pJsonRoot, "RESOLUTION", rt_data.resolution);
	cJSON_AddNumberToObject(pJsonRoot, "RECORD_ENABLE", rt_data.record_enable);
	cJSON_AddNumberToObject(pJsonRoot, "LIGHT_ENABLE", rt_data.light_enable);
	cJSON_AddNumberToObject(pJsonRoot, "TEMPERATURE", rt_data.temperature);
	cJSON_AddNumberToObject(pJsonRoot, "ALARM_FLAG", rt_data.alarm_flag);

	cJSON_AddNumberToObject(pJsonRoot, "FRAME_RATE", rt_data.frame_rate);
	cJSON_AddNumberToObject(pJsonRoot, "BIT_RATE", rt_data.bit_rate);
	cJSON_AddNumberToObject(pJsonRoot, "VIDEO_QUALITY", rt_data.video_quality);
	cJSON_AddNumberToObject(pJsonRoot, "SAMPLE_RATE", rt_data.sample_rate);

	p = cJSON_Print(pJsonRoot);
	if (NULL == p)
	{
		cJSON_Delete(pJsonRoot);
		goto err;
	}
	cJSON_Delete(pJsonRoot);
	return p;

err:
	return 0;
}

class MQTTData : public QDataBase
{
public:
	char from[128];
	char msg[256];
	MQTTData(){};
	~MQTTData(){};
	virtual int process()
	{
		printf("%s %s\n", from, msg);
		cJSON *pSub;
		cJSON *pJson;
		if (NULL == msg)
		{
			return 0;
		}
		pJson = cJSON_Parse(msg);
		if (NULL == pJson)
		{
			return 0;
		}

		pSub = cJSON_GetObjectItem(pJson, "MSGID");
		if (NULL != pSub)
		{
			printf("UUID:%s\n", pSub->valuestring);
			char *str = get_xintiao_msgbody(pSub->valuestring);
			pubmsg(str);
			free(str);
		}

		cJSON_Delete(pJson);
	}
};

class MQTTXinTiaoData : public QDataBase
{
public:
	MQTTXinTiaoData(){};
	~MQTTXinTiaoData(){};
	virtual int process()
	{
		char *str = get_xintiao_msgbody(get_mqtt_uuid());
		pubmsg(str);
		free(str);
	}
};

int sendxintiao()
{
	MQTTXinTiaoData *xt = new MQTTXinTiaoData();
	producter_push(xt);
}




lpFunc mng_fun;
void device_state_mng(lpFunc fun)
{
	mng_fun=fun;
}
/*本地MQTT接收到数据的回调函数*/
int mqttArrived_Fun(char *from, char *msg)
{
	printf("%s %d %s %s\r\n", __FILE__,__LINE__,from, msg);

	mng_fun(from,msg);
	/*
	int recv_streamstat=-1,recv_storge=-1;
	char recv_push_url[1024];
	cJSON *pJson;
	cJSON *pSub;
	if (NULL == msg)
	{
		return 0;
	}
	pJson = cJSON_Parse(msg);
	if (NULL == pJson)
	{
		printf("pJson is NULL\r\n");
		return 0;
	}

	pSub = cJSON_GetObjectItem(pJson, "url");
	if(pSub==NULL)
	{
		printf("pSub is NULL\r\n");
	}
	else
	{
		sprintf(recv_push_url,"%s",pSub->valuestring);
		printf("url:%s\n", recv_push_url);
	}

	pSub = cJSON_GetObjectItem(pJson, "STREAM");
	if(pSub==NULL)
	{
		printf("pSub is NULL\r\n");
	}
	else
	{
		recv_streamstat=pSub->valueint;
		printf("recv_streamstat:%d\n", recv_streamstat);
	}
	
	pSub = cJSON_GetObjectItem(pJson, "STORAGE");
	if(pSub==NULL)
	{
		printf("pSub is NULL\r\n");
	}
	else
	{
		recv_storge=pSub->valueint;
		printf("recv_streamstat:%d\n", recv_storge);
	}
	*/

	
	
	//cJSON_Delete(pJson);
	
#if 0
	MQTTData *md = new MQTTData();
	snprintf(md->from, sizeof(md->from), "%s", from);
	snprintf(md->msg, sizeof(md->msg), "%s", msg);
	producter_push(md);
#endif
	return 0;
}

int MQTTPROCESS::run()
{

	for (;;)
	{
		RUN_TEST;
		if (mqttcd.conn == 1)
		{
			RUN_TEST;
			//如果连接正常，将发送心跳包
			sendxintiao();
			sleep(10);
		}
		else
		{

			sleep(1);
		}
	}
	return 0;
}